package cn.edu.scau.cmi.lijiongtao.client;

import java.util.ArrayList;

import cn.edu.scau.cmi.lijiongtao.singleton.Multipleton;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class MultipletonClient extends Application {
	private ArrayList<Multipleton> marshalList = new ArrayList<Multipleton>();
	private String[] marshalsName = {"朱德", "彭德怀", "林彪", "刘伯承", "贺龙", "陈毅", "罗荣桓", "徐向前", "聂荣臻", "叶剑英" };
	private int num = 0;
	
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		primaryStage.setTitle("实验二：多例模式");
		primaryStage.setHeight(300);
		primaryStage.setWidth(317);
		
		Button bt1 = new Button("随机获取一个十大元帅");
		bt1.setMinWidth(300);
		bt1.setMinHeight(50);
		
		HBox hBox = new HBox();
		hBox.getChildren().addAll(bt1);
		
		Label lb = new Label();
		
		BorderPane bp = new BorderPane();
		bp.setBottom(hBox);
		bp.setCenter(lb);
		
		Scene s = new Scene(bp,480,600);
		primaryStage.setScene(s);
		primaryStage.show();
		
		bt1.setOnAction(new EventHandler<ActionEvent> () {
			@Override
			public void handle(ActionEvent arg0) {
				if(marshalList.size() < 10) {
					lb.setText("");
					marshalList = Multipleton.addInstance(marshalsName[num++]);
					for(int i = 0; i < marshalList.size(); i++) {
						lb.setText(lb.getText() + marshalList.get(i).getName() + "\n");
					}
				}
			}
		});
		
	}

}
